package org.ctsi.rsds.base.rest;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;

/**
 * 参数对象与json之间序列化和反序列化的转换
 * @author kevin
 * @version 1.0-dev 2016年5月12日
 */
@Provider
public class MyResteasyJacksonJsonProvider extends JacksonJsonProvider {

	/**
	 * 重写父类的序列化方法，在父类方法前做相应的设置
	 */
	@Override
	public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
		ObjectMapper mapper = locateMapper(type, mediaType);

		// 序列化
		 mapper.getSerializationConfig()
		 	   .withSerializationInclusion(JsonSerialize.Inclusion.NON_NULL)
		 	   .withDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
		 mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false);

		super.writeTo(value, type, genericType, annotations, mediaType, httpHeaders, entityStream);
	}

	/**
	 * 重写父类的反序列化方法，在父类方法前做相应的设置
	 */
	@Override
	public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {

		ObjectMapper mapper = locateMapper(type, mediaType);
		
		// 反序列化
		mapper.getDeserializationConfig()
			  .withDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		
		return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
	}

}
